package com.seari.ats_iscs.handler;

import com.seari.ats_iscs.AtsIscsClient;
import com.seari.ats_iscs.AtsIscsHelper;
import com.seari.bean.ats_iscs.AtsIscsMessageTypeEnum;
import com.seari.commonutils.common.DataUtils;
import com.seari.utils.DataUtil;
import io.netty.buffer.ByteBuf;
import org.slf4j.LoggerFactory;

/**
 * 更新订阅报文线程
 *
 * @Author: zyk
 * @Date: 2019/5/29 18:07
 * @Version 1.0
 */
public class AtsIscsTimer implements Runnable {

    //终止线程
    private boolean terminate = false;
    //清除
    private boolean reset = false;
    //客户端
    private AtsIscsClient client;
    //身份识别号
    private byte[] identityNumber;
    //日志
    protected static org.slf4j.Logger logger = LoggerFactory.getLogger(AtsIscsTimer.class);

    public AtsIscsTimer(AtsIscsClient client, byte[] identityNumber, Boolean reset) {
        this.client = client;
        this.identityNumber = identityNumber;
        this.reset = reset;
    }

    @Override
    public void run() {
        //是否终止线程
//        while (!terminate) {
//            try {
//                TimeUnit.SECONDS.sleep(20);
//                //是否清除发送内容
//                if (!reset) {
//                    logger.debug("timer is not actived");
//                    continue;
//                }
                ByteBuf buffer = client.channel.alloc().buffer((4 + 2 + 2 + 4) * 4);
                buffer.writeBytes(AtsIscsHelper.ATS_ISCS_HEADER);
                //设置报文长度
                buffer.writeBytes(DataUtil.reverse(DataUtils.int16ToByteArray(6)));
                //设置报文类型
                buffer.writeBytes(DataUtil.reverse(DataUtil.short2byte(AtsIscsMessageTypeEnum.UPDATE_SUBSCRIBE.getValue())));
                //添加身份识别号
                buffer.writeBytes(identityNumber);
                //发送报文
                client.channel.writeAndFlush(buffer);
                logger.info("ATS_ISCS#########发送更新订阅报文##########");
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
    }

    public boolean isTerminate() {
        return terminate;
    }

    public void setTerminate(boolean terminate) {
        this.terminate = terminate;
    }

    public boolean isReset() {
        return reset;
    }

    public void setReset(boolean reset) {
        this.reset = reset;
    }
}
